//-----------------------------------------------------------------------
// <copyright file="BannerCatDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class BannerCatDAOBase
    {
        #region Common methods
        public virtual BannerCat CreateBannerCatFromReader(IDataReader reader)
        {
            BannerCat item = new BannerCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("LocationId"))) item.LocationId = (long)reader["LocationId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubLocationId"))) item.SubLocationId = (long)reader["SubLocationId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Level"))) item.Level = (int)reader["Level"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartusId"))) item.StartusId = (int)reader["StartusId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerCatFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateBannerCat methods
            
        public virtual void CreateBannerCat(BannerCat bannerCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerCat.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerCat.Description);
                database.AddInParameter(dbCommand, "@SubLocationId", DbType.Int64, bannerCat.SubLocationId);
                database.AddInParameter(dbCommand, "@Level", DbType.Int32, bannerCat.Level);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerCat.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerCat.Enable);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerCat.StartusId);
                database.AddOutParameter(dbCommand, "@LocationId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                bannerCat.LocationId = (long)database.GetParameterValue(dbCommand, "@LocationId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerCatException, ex);
            }
        }

        #endregion

        #region UpdateBannerCat methods
        
        public virtual void UpdateBannerCat(BannerCat bannerCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatUpdate");            
                
                database.AddInParameter(dbCommand, "@LocationId", DbType.Int64, bannerCat.LocationId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerCat.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerCat.Description);
                database.AddInParameter(dbCommand, "@SubLocationId", DbType.Int64, bannerCat.SubLocationId);
                database.AddInParameter(dbCommand, "@Level", DbType.Int32, bannerCat.Level);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerCat.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerCat.Enable);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerCat.StartusId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBannerCatException, ex);
            }
        }
        
        #endregion

        #region DeleteBannerCat methods
        public virtual void DeleteBannerCat(long locationId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatDelete");
                
                database.AddInParameter(dbCommand, "@LocationId", DbType.Int64, locationId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBannerCatException, ex);
            }
        }

        #endregion

        #region GetBannerCat methods
        
        public virtual BannerCat GetBannerCat(long locationId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatGet");
                
                database.AddInParameter(dbCommand, "@LocationId", DbType.Int64, locationId);
                
                BannerCat bannerCat =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        bannerCat = CreateBannerCatFromReader(reader);
                        reader.Close();
                    }
                }
                return bannerCat;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerCatException, ex);
            }
        }
        
        #endregion

        #region GetBannerCatList methods
        public virtual BannerCatCollection GetBannerCatList(BannerCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerCatCollection bannerCatCollection = new BannerCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerCat bannerCat = CreateBannerCatFromReader(reader);
                        bannerCatCollection.Add(bannerCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerCatListException, ex);
            }
        }
        
        public virtual BannerCatCollection GetBannerCatList(BannerCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBannerCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetBannerCatList methods
        public virtual BannerCatCollection GetBannerCatListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerCatGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerCatCollection bannerCatCollection = new BannerCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerCat bannerCat = CreateBannerCatFromReader(reader);
                        bannerCatCollection.Add(bannerCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerCatListException, ex);
            }
        }
        
        public virtual BannerCatCollection GetBannerCatListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetBannerCatListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}